<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="btns">
        <button data-path="/home">首页</button>
        <button data-path="/news">新闻</button>
        <button data-path="/music">音乐</button>
    </div>
    <div id="content"></div>
    <script>
        const oContent = document.getElementById("content");
        const routes = [
            { path: "/home", component: "我是首页" },
            { path: "/news", component: "我是新闻" },
            { path: "/music", component: "我是新歌" },
        ]

        const oBtns = document.querySelectorAll(".btns button");
        oBtns.forEach((item) => {
            item.onclick = function () {
                location.hash = this.dataset.path;
            }
        })

        window.onhashchange = function (e) {
            console.log(e);
            const newHash = e.newURL.split("#")[1];
            routes.forEach((item) => {
                if (item.path === newHash) {
                    oContent.innerHTML = item.component;
                }
            });
        };
    </script>
</body>

</html>